package com.expensetracker.dto;

import static javax.persistence.GenerationType.SEQUENCE;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name="transaction")
public class ExpenseTransaction implements Serializable{

	private static final long serialVersionUID = 2640803938664657464L;
	private Long id;
	private User user;
	private PlaceOfPurchase placeOfPurchase;
	private Date purchaseDate;
	private Collection<Expense> expenses = new ArrayList<Expense>();
	
	@SequenceGenerator(name = "generator", sequenceName = "transaction_id_seq")
	@Id
	@GeneratedValue(strategy = SEQUENCE, generator = "generator")
	@Column(name = "ID", unique = true, nullable = false)
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	
	@ManyToOne()
	@JoinColumn(name="USER_ID")
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	
	@ManyToOne()
	@JoinColumn(name="PLACE_OF_PURCHASE")
	public PlaceOfPurchase getPlaceOfPurchase() {
		return placeOfPurchase;
	}
	public void setPlaceOfPurchase(PlaceOfPurchase placeOfPurchase) {
		this.placeOfPurchase = placeOfPurchase;
	}
	
	@Temporal(TemporalType.DATE)
	@Column(name = "PURCHASE_DATE", nullable = false)
	public Date getPurchaseDate() {
		return purchaseDate;
	}
	public void setPurchaseDate(Date purchaseDate) {
		this.purchaseDate = purchaseDate;
	}
	
	@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER, mappedBy = "transaction")
	public Collection<Expense> getExpenses() {
		return expenses;
	}
	public void setExpenses(Collection<Expense> expenses) {
		this.expenses = expenses;
	}
	
	
	
	
}
